package com.ray.business.strategy.order.create;

import cn.hutool.core.util.StrUtil;
import com.ray.business.builder.BusinessBuilder;
import com.ray.business.builder.BusinessInBuilder;
import com.ray.business.check.BusinessOutCheck;
import com.ray.business.enums.BusinessStatusEnum;
import com.ray.business.enums.BusinessTypeEnum;
import com.ray.business.enums.OrderStatusEnum;
import com.ray.business.service.ProdBusinessInService;
import com.ray.business.service.ProdBusinessOutService;
import com.ray.business.service.ProdBusinessService;
import com.ray.business.table.entity.ProdBusinessIn;
import com.ray.business.table.entity.ProdBusinessOut;
import com.ray.business.table.params.business.BusinessCreateParams;
import com.ray.common.SysMsgCodeConstant;
import com.ray.magicBlock.Strategy;
import com.ray.magicBlock.anno.Block;
import com.ray.validate.support.utils.ValidateUtil;
import com.ray.woodencreate.beans.LoginUser;
import com.ray.woodencreate.exception.BusinessExceptionFactory;
import com.ray.woodencreate.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bo shen
 * @Description: 订单商品数据
 * @Class: PurchaseInService
 * @Package com.ray.wms.strategy.in
 * @date 2020/6/8 15:05
 * @company <p>Ray快速开发平台</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
@Block(group = "createBusiness", strategy = "in", desc = "工艺商品物料新增")
public class InOrderCreateService implements Strategy<BusinessCreateParams, LoginUser, String> {

    @Autowired
    private ProdBusinessService prodBusinessService;
    @Autowired
    private ProdBusinessInService prodBusinessInService;
    @Autowired
    private ProdBusinessOutService prodBusinessOutService;

    @Override
    public String execute(BusinessCreateParams createParams, LoginUser loginUser) {
        BusinessBuilder businessBuilder = new BusinessBuilder();
        businessBuilder.append(createParams).appendType(BusinessTypeEnum.IN.getValue())
                .appendStatus(BusinessStatusEnum.UN_CHECK.getValue()).appendCreate(loginUser);
        final List<BigDecimal> quantity = new ArrayList<>();
        //保存物料明细
        List<ProdBusinessIn> inList = createParams.getGoods().stream().map(businessGoodsParams -> {
            BusinessInBuilder businessInBuilder = new BusinessInBuilder().append(businessGoodsParams)
                    .appendBusinessCode(businessBuilder.getCode()).appendCreate(loginUser);
            //业务入库必填序列号
            Assert.hasLength(businessGoodsParams.getSerialNumber(),"请填写序列号");
            //判断是否关联唯一单号
            if (StrUtil.isNotBlank(businessGoodsParams.getCode())) {
                //查询数据信息
                ProdBusinessOut businessOut = prodBusinessOutService.getUsableGoodsByCode(businessGoodsParams.getCode(), loginUser);
                new BusinessOutCheck(businessOut).checkNull("对应业务出库记录不存在");
                //更新使用状态
                if (!prodBusinessOutService.useBusinessOut(businessGoodsParams.getCode(), businessOut.getUpdateVersion(), loginUser)) {
                    log.info("更新记录失败:{}", businessGoodsParams.getCode());
                    throw BusinessExceptionFactory.newException("更新记录异常");
                }
                businessInBuilder.appendOutCode(businessOut.getCode());
            }
            quantity.add(businessGoodsParams.getQuantity());
            return businessInBuilder.bulid();
        }).collect(Collectors.toList());
        //保存总数
        businessBuilder.append(quantity.stream().reduce(BigDecimal::add).get());
        //保存
        prodBusinessService.save(businessBuilder.bulid());
        //保存数据
        prodBusinessInService.saveBatch(inList);
        return businessBuilder.getCode();
    }
}
